package com.baizhi.web_demo;

import java.util.Arrays;

/**
 * 正则表达式的其他方法
 */
public class RegMethod01 {
    public static void main(String[] args) {
       /*
            String[] split(String regex)
            将当前字符串按照满足正则表达式的部分进行拆分，得到的结果以数组形式返回
        */
        String str = "abc123def456hig78910klm";
        //需求：得到str中所有的字符
        // 就是按照指定的正则表达式进行拆分， 按照数字进行拆分
        String reg = "[0-9]+";

        String[] strs = str.split(reg);
        System.out.println(strs.length);
        System.out.println(Arrays.toString(strs));


        String str2 = "abc,def,hig,kml";
        String[] data = str2.split(",");
        System.out.println(Arrays.toString(data));


        String str3 = "abc.def.hig.kml";
        data = str3.split("\\.");
        System.out.println(Arrays.toString(data));

        System.out.println("--------------------------");


        /*
          如果字符串开始位置就是可拆分享时，会先拆分出一个空字符串
         */
        String str4 = ",abc,def,hig,kml";
        data = str4.split(",");
        System.out.println(data.length); //5
        System.out.println(Arrays.toString(data));


        /*
          如果字符串中间位置有连续可拆分项时，会先拆分出多个空字符串
         */
        String str5 = "abc,,def,hig,kml";
        data = str5.split(",");
        System.out.println(data.length); //5
        System.out.println(Arrays.toString(data));


          /*
          如果字符串末尾位置有连续可拆分享时，会被忽略
         */
        String str6 = "abc,def,hig,kml,,,,,,,";
        data = str6.split(",");
        System.out.println(data.length); //4
        System.out.println(Arrays.toString(data));


    }
}
